package edu.zut.cs.software.module.commodity.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.module.commodity.domain.Commodity;
import edu.zut.cs.software.teach.Base.service.GenericGenerator;

/**
 * 
 * @author gaoliucheng
 *
 */

public class CommodityEntityGenerator extends GenericGenerator{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(CommodityEntityGenerator.class.getName());
	
	
	
List<Commodity> commodityList;
	
	CommodityManager commoditymanager;
	

	@Autowired
	public void setCommoditycategorymanager(CommodityManager commoditymanager) {
		this.commoditymanager = commoditymanager;
	}

	@Before
	public void setUp() throws Exception {
		this.commodityList = new ArrayList<Commodity>();
		InputStream input = CommodityManagerTest.class.getResourceAsStream("/sms_commodity_category.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int  i=1;i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Commodity s = new Commodity();
			for (int j= 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setName(value);
				}
			}
			this.commodityList.add(s);
		}
	}
	
	@Test
	public void test() {
		this.commoditymanager.deleteAll();
		this.commoditymanager.save(this.commodityList);
		Iterable<Commodity> result = this.commoditymanager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - Iterable<Commodity> result={}", result); //$NON-NLS-1$
		}

	
	}

}

